package com.example.deertrip;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.deertrip.bean.Tb_HotelReserve;
import com.example.deertrip.database.DBOperate;

import java.util.HashMap;

public class activity_pay_Activity extends AppCompatActivity {

    private String username;
    public String getusername(){
        return username;
    }
    public void setusername(String username){
        this.username=username;
    }

    private String pway;
    public String getpway(){
        return pway;
    }
    public void setpway(String pway){
        this.pway=pway;
    }


    private String yje;             //订单金额
    public String getyje(){
        return yje;
    }
    public void setyje(String yje){
        this.yje=yje;
    }

    private String Banlance;
    public String getBanlance(){
        return Banlance;
    }
    public void setBanlance(String Banlance){
        this.Banlance=Banlance;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        DBOperate dbOperate=new DBOperate(activity_pay_Activity.this);

        //订单信息获取
        TextView t1=findViewById(R.id.tv_72);
        TextView t2=findViewById(R.id.tv_84);
        TextView t3=findViewById(R.id.tv_32);
        TextView t4=findViewById(R.id.tv_42);
        TextView t5=findViewById(R.id.tv_52);
        TextView t6=findViewById(R.id.tv_82);

        SharedPreferences sp=getSharedPreferences("deertrip",MODE_PRIVATE);    //获取preference里的用户名
        String uname=sp.getString("UserName","");
        Bundle bundle=getIntent().getExtras();
        if(bundle!=null){
            t1.setText((String)bundle.get("HotelName"));
            t2.setText((String)bundle.get("RoomNumber"));
            t3.setText((String)bundle.get("HotelOrderPrice"));
            t4.setText((String)bundle.get("lindate"));
            t5.setText((String)bundle.get("loutdate"));
            t6.setText((String)bundle.get("RoomType"));

            setyje((String)bundle.get("HotelOrderPrice"));
            setusername(uname);
        }

        //spinner_in
        Spinner spinner_in=findViewById(R.id.spipayway);
        spinner_in.setSelection(0);		//初始化，默认选择列表中第0个元素
        spinner_in.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                // TODO

                if (pos==1){//银行卡
                    setpway("1");
                }
                else if(pos==2){//支付宝
                    setpway("2");
                }
                else{//微信
                    setpway("0");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                setpway("0");
                // TODO
            }

        });

        Button button_confirmbuy;
        button_confirmbuy=(Button) findViewById(R.id.bu_1);

        button_confirmbuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String selectway=getpway();
                SharedPreferences sp=getSharedPreferences("deertrip",MODE_PRIVATE);
                String yue=null;
                if(selectway.equals("0"))       //查wx余额
                {
                    yue=sp.getString("WxBalance","0");
                }
                else if(selectway.equals("1"))    //查zfb余额
                {
                    yue=sp.getString("YhkBalance","0");
                }
                else
                {
                    yue=sp.getString("ZfbBalance","0");
                }


                int v1=Integer.parseInt(getyje());
                int v2=Integer.parseInt(yue);

                int rnum=Integer.parseInt((String)bundle.get("RoomNumber") );
                if(v1 <= v2){        //订单金额小于等于余额
                    int nowBalance=v2-v1;           //更新余额
                    SharedPreferences.Editor editor = sp.edit();
                    String nowba=String.valueOf(nowBalance);
                    if(selectway.equals("0"))       //查wx余额
                    {
                        editor.putString("WxBalance",nowba);
                    }
                    else if(selectway.equals("1"))    //查zfb余额
                    {
                        editor.putString("YhkBalance",nowba);
                    }
                    else
                    {
                        editor.putString("ZfbBalance",nowba);
                    }
                    editor.commit();

                    //创建订单
                    Tb_HotelReserve Hotelorder=new Tb_HotelReserve(rnum,(String)bundle.get("HotelName"),v1,(String)bundle.get("lindate"),(String)bundle.get("loutdate"),(String)bundle.get("RoomType"),getusername());
                    dbOperate.saveHotelReserveInfo(Hotelorder);
                    Intent intent=new Intent(activity_pay_Activity.this, Pay_Suc_Activity.class);
                    startActivity(intent);
                }
                else{
                    Intent intent=new Intent(activity_pay_Activity.this, Pay_Fai_Activity.class);
                    startActivity(intent);
                }
            }
        });
    }

}